package com.bootdo.common.utils;



import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateFormatUtil {
    public static String FormatDate(String dateStr) {
        HashMap<String, String> dateRegFormat = new HashMap<String, String>();
        dateRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D*$",
                "yyyy-MM-dd-HH-mm-ss");// 2014��3��12�� 13ʱ5��34�룬2014-03-12
        // 12:05:34��2014/3/12 12:5:34
        dateRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$", "yyyy-MM-dd-HH-mm");// 2014-03-12
        // 12:05
        dateRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$", "yyyy-MM-dd-HH");// 2014-03-12
        // 12
        dateRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}$", "yyyy-MM-dd");// 2014-03-12
        dateRegFormat.put("^\\d{4}\\D+\\d{1,2}$", "yyyy-MM");// 2014-03
        dateRegFormat.put("^\\d{4}$", "yyyy");// 2014
        dateRegFormat.put("^\\d{14}$", "yyyyMMddHHmmss");// 20140312120534
        dateRegFormat.put("^\\d{12}$", "yyyyMMddHHmm");// 201403121205
        dateRegFormat.put("^\\d{10}$", "yyyyMMddHH");// 2014031212
        dateRegFormat.put("^\\d{8}$", "yyyyMMdd");// 20140312
        dateRegFormat.put("^\\d{6}$", "yyyyMM");// 201403
        dateRegFormat.put("^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$", "yyyy-MM-dd-HH-mm-ss");// 13:05:34
        // ƴ�ӵ�ǰ����
        dateRegFormat.put("^\\d{2}\\s*:\\s*\\d{2}$", "yyyy-MM-dd-HH-mm");// 13:05
        // ƴ�ӵ�ǰ����
        dateRegFormat.put("^\\d{2}\\D+\\d{1,2}\\D+\\d{1,2}$", "yy-MM-dd");// 14.10.18(��.��.��)
        dateRegFormat.put("^\\d{1,2}\\D+\\d{1,2}$", "yyyy-dd-MM");// 30.12(��.��)
        // ƴ�ӵ�ǰ���
        dateRegFormat.put("^\\d{1,2}\\D+\\d{1,2}\\D+\\d{4}$", "dd-MM-yyyy");// 12.21.2013(��.��.��)

        String curDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        DateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat formatter2;
        String dateReplace;
        String strSuccess = "";
        try {
            for (String key : dateRegFormat.keySet()) {
                if (Pattern.compile(key).matcher(dateStr).matches()) {
                    formatter2 = new SimpleDateFormat(dateRegFormat.get(key));
                    if (key.equals("^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$") || key.equals("^\\d{2}\\s*:\\s*\\d{2}$")) {
                        dateStr = curDate + "-" + dateStr;
                    } else if (key.equals("^\\d{1,2}\\D+\\d{1,2}$")) {
                        dateStr = curDate.substring(0, 4) + "-" + dateStr;
                    }
                    dateReplace = dateStr.replaceAll("\\D+", "-");
                    strSuccess = formatter1.format(formatter2.parse(dateReplace));
                    break;
                }
            }
        } catch (Exception e) {
            System.err.println("-----------------日期格式无效:" + dateStr);
            throw new Exception("日期格式无效");
        } finally {
            return strSuccess;
        }
    }

    public static String string2num(String timeString) {
        String str = timeString.trim();
        String str2 = "";
        if (str != null && !"".equals(str)) {
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
                    str2 += str.charAt(i);
                }
            }
        }
        return str2;
    }

    public static String dateStr2Timestamp(String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStamp = "";
        try {
            timeStamp = String.valueOf(sdf.parse(dateStr).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }

    public static String getfirstDate(String url) {

        if (url.contains("//")) {
            try {
                java.net.URL  neturl = new  java.net.URL(url);
                url= neturl.getFile();
                System.out.println("++++++++++++++++"+url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }

        String pattern = "/[0-9]{2,4}[/,-]?[0-9]{2}[/,-]?[0-9]{2}/";

        //String pattern = "\\d+";
        //Regex regex=new Regex("[0-9]{4}-(1[0,1,2]|[1-9])-(0[1-9]|[1,2][0-9]|3[0,1])");

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(url);
        if (m.find()) {
            url = m.group(0);
            System.out.println("+++++++timestr+++++++++"+url);
        } else {
            System.out.println("++++++++no match++++++++"+url);
            url = null;
        }

        return url;
    }
    public static String getdate(String url) {

        String str2="";
        if(url != null && !"".equals(url)){
            url=url.trim();
            System.out.println("++++++++++++++++"+url);
            for(int i=0;i<url.length();i++){
                if(url.charAt(i)>=48 && url.charAt(i)<=57){
                    str2+=url.charAt(i);
                }
            }
        }
        int i = str2.length();
        if (i==6) {
            StringBuilder sb = new StringBuilder(str2);
            sb.insert(0,"20");
            str2 = sb.toString();
        }
        System.out.println(str2);
        if (str2.length() != 8 ) {

            return  null;
        }
        //String urlll = str2.substring(str2.indexOf("20"),str2.indexOf("20")+8);
        // System.out.println(urlll);
        return str2;
    }

    public static void main(String[] args) {
        String[] dateStrArray = new String[] {
                "时间：2014-03-12 12:05:34",
                "时间：2014-03-12 12:05:34",
                "2014-03-12 12:05:34",
                "2014-03-12 12:05",
                "2014-03-12 12",
                "2014-03-12",
                "2014-03",
                "2014",
                "20140312120534",
                "2014/03/12 12:05:34",
                "2014/3/12 12:5:34",
                "2014年3月12日 13时5分34秒",
                "201403121205",
                "1234567890",
                "20140312",
                "201403",
                "2000 13 33 13 13 13",
                "30.12.2013",
                "12.21.2013",
                "21.1",
                "13:05:34",
                "12:05",
                "14.1.8",
                "14.10.18",
                "发表日期： 2017-08-11 07:54",
                "2017/3/1"};
        for (int i = 0; i < dateStrArray.length; i++) {
            String dateTime = FormatDate(dateStrArray[i]);
            if(dateTime.length() == 0){
                dateTime = FormatDate(string2num(dateStrArray[i]));
            }
            System.out.println(dateStrArray[i] + "====>"+ dateTime);
        }
    }
}
